package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @作者 itcast
 * @创建日期 2020/10/8 9:27
 **/
@Service
@Transactional
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper,ApBehaviorEntry> implements ApBehaviorEntryService {
    @Override
    public ApBehaviorEntry findByUserIdOrEquipmentId(Integer userId, Integer equipmentId) {
        // 先判断 userId是否为空  不为空按照userId来查行为实体
        if(userId != null && userId != 0){
            return getOne(
                    Wrappers.<ApBehaviorEntry>lambdaQuery()
                            .eq(ApBehaviorEntry::getEntryId,userId)
                            .eq(ApBehaviorEntry::getType,1));
        }
        // userId为空的话，判断equipmentId是否为空，不为空按照设备ID来查行为实体
        if(equipmentId !=null && equipmentId !=0 ){
            return getOne(
                    Wrappers.<ApBehaviorEntry>lambdaQuery()
                            .eq(ApBehaviorEntry::getEntryId,equipmentId)
                            .eq(ApBehaviorEntry::getType,0));
        }
        return null;
    }
}
